package com.fc.controller;

import com.fc.anno.Log;
import com.fc.context.BaseContext;
import com.fc.dto.UserDTO;
import com.fc.dto.UserLoginDTO;
import com.fc.dto.UserRegisterDTO;
import com.fc.entity.User;
import com.fc.result.Result;
import com.fc.service.IUserService;
import com.fc.vo.UserLoginVO;
import com.fc.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "用户")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private IUserService userService;


    /**
     * 发送验证码
     *
     * @return
     */
    @ApiOperation("发送验证码")
    @GetMapping("/sendVerificationCode")
    public Result<Map<String, Object>> sendCode() {
        //返回验证码图片
        Map<String, Object> captcha = userService.sendCode();
        return Result.success(captcha);
    }

    /**
     * 注册
     *
     * @return
     */
    @ApiOperation("注册")
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO) {
        userService.register(userRegisterDTO);
        return Result.success();
    }

    /**
     * 登录
     *
     * @param userLoginDTO
     * @return
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        UserLoginVO userLoginVO = userService.login(userLoginDTO);
        return Result.success(userLoginVO);
    }

    /**
     * 登出
     *
     * @param request 前端请求
     * @return
     */
    @ApiOperation("登出")
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request) {
        userService.logout(request);
        return Result.success();
    }

    /**
     * 修改用户
     *
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("修改用户")
    @PutMapping
    public Result update(@RequestBody UserDTO userDTO) {
        userService.updateUser(userDTO);
        return Result.success();
    }

    /**
     * 删除用户
     *
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        userService.removeUser(id);
        return Result.success();
    }

    /**
     * 新增用户
     *
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("新增用户")
    @PostMapping
    public Result<List<User>> save(@RequestBody UserDTO userDTO) {
        userService.saveUser(userDTO);
        return Result.success();
    }

    /**
     * 查询用户列表
     *
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("查询用户列表")
    @GetMapping("/list")
    public Result<List<UserVO>> list(String username, String name) {
        List<UserVO> userVOList = userService.listUser(username, name);
        return Result.success(userVOList);
    }


    /**
     * 查看用户详细用户
     *
     * @return
     */

    @ApiOperation("查询用户详细信息")
    @GetMapping("/{id}")
    public Result<User> detail(@PathVariable Long id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 用户自身信息查询
     *
     * @return
     */
    @ApiOperation("用户自身信息查询")
    @GetMapping
    public Result<User> detail() {
        User user = userService.getById(BaseContext.getCurrentId());
        return Result.success(user);
    }

    /**
     * 管理员重置密码
     *
     * @param id
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("重置密码")
    @GetMapping("/reset/{id}")
    public Result reset(@PathVariable Long id) {
        userService.reset(id);
        return Result.success();
    }

}